var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var UserReviseController = (function (_super) {
    __extends(UserReviseController, _super);
    function UserReviseController($scope, userService, rolesService, valueService, storageService, $ngBootbox, azureBlob, $stateParams) {
        var _this = this;
        _super.call(this, $scope);
        this.userService = userService;
        this.rolesService = rolesService;
        this.valueService = valueService;
        this.storageService = storageService;
        this.azureBlob = azureBlob;
        this.$stateParams = $stateParams;
        /**
         * 加载roles角色下拉
         */
        this.loadRolesCheckBox = function () {
            _this.rolesService.getList().then(function (result) {
                _this.$scope.rolesList = result.data.list;
            });
        };
        /**
         * 显示强制修改用户(编辑)
         */
        this.showReviseUserDialog = function (user) {
            _this.userService.getDetail(user.userId).then(function (result) {
                _this.$scope.detail = {
                    userId: user.userId,
                    carCode: result.data.carCode,
                    userName: user.realName || user.userName || user.phoneNumber,
                    realHeadImageStatus: _this.valueService.getCheckStateList().list[result.data.realHeadState].text,
                    idCardImageSatus: _this.valueService.getCheckStateList().list[result.data.idCardState].text,
                    drivingLicenseImageStatus: _this.valueService.getCheckStateList().list[result.data.drivingLicenseState].text,
                    vehicleLicenseImageStatus: _this.valueService.getCheckStateList().list[result.data.vehicleLicenseState].text,
                    realHeadImage: result.data.realHeadImage || "img/a1.jpg",
                    idCardImage: result.data.idCardImage || "img/a1.jpg",
                    drivingLicenseImage: result.data.drivingLicenseImage || "img/a1.jpg",
                    vehicleLicenseImage: result.data.vehicleLicenseImage || "img/a1.jpg",
                    realHeadImageTool: result.data.realHeadState == 2 && result.data.realHeadImage !== null,
                    idCardImageTool: result.data.idCardState == 2 && result.data.idCardImage !== null,
                    drivingLicenseImageTool: result.data.drivingLicenseState == 2 && result.data.drivingLicenseImage !== null,
                    vehicleLicenseImageTool: result.data.vehicleLicenseState == 2 && result.data.vehicleLicenseImage !== null,
                    clickOkPassState: true,
                    clickRemovePassState: true,
                    clickIdCardOkPassState: true,
                    clickIdCardRemovePassState: true,
                    clickDrivingLicenseOkPassState: true,
                    clickDrivingLicenseRemovePassState: true,
                    clickVehicleLicenseOkPassState: true,
                    clickVehicleLicenseRemovePassState: true,
                    drivingLicenseCertificated: result.data.drivingLicenseState,
                    realHeadImageCertificated: result.data.realHeadState,
                    idCardCertificated: result.data.idCardState,
                    vehicleLicenseCertificated: result.data.vehicleLicenseState,
                    message: result.data.message,
                    clickOk: function (statusValue) { return _this.statePassCheck('pass', statusValue, false); },
                    clickOkPass: function (statusValue) { return _this.statePassCheck('pass', statusValue, true); },
                    clickRemove: function (statusValue) { return _this.statePassCheck('remove', statusValue, null, false); },
                    clickRemovePass: function (statusValue) { return _this.statePassCheck('remove', statusValue, null, true); },
                    rolesCheckedList: _this.rolesChecked(user.roles),
                    realHeadUpload: result.data.realHeadState == 3 || result.data.realHeadState == 4 || result.data.realHeadImage !== null,
                    idCardUpload: result.data.idCardState == 3 || result.data.idCardState == 4 || result.data.realHeadImage !== null,
                    drivingLicenseUpload: result.data.drivingLicenseState == 3 || result.data.drivingLicenseState == 4 || result.data.drivingLicenseImage !== null,
                    vehicleLicenseUpload: result.data.vehicleLicenseState == 3 || result.data.vehicleLicenseState == 4 || result.data.vehicleLicenseImage !== null,
                    submitRealHeadUrl: '',
                    submitIdCardUrl: "",
                    submitDrivingLicenseUrl: '',
                    submitVehicleLicenseUrl: '',
                    selectedFileRealHead: null,
                    selectedFileIdCard: null,
                    selectedFileDrivingLicense: null,
                    selectedFileVehicleLicense: null,
                    sasUrl: _this.getSasUrl(),
                    uploaderRealHead: function () { return _this.upload(_this.$scope.detail.submitRealHeadUrl, _this.$scope.detail.selectedFileRealHead, 'RealHead'); },
                    uploaderIdCard: function () { return _this.upload(_this.$scope.detail.submitIdCardUrl, _this.$scope.detail.selectedFileIdCard, 'IDCard'); },
                    uploaderDrivingLicense: function () { return _this.upload(_this.$scope.detail.submitDrivingLicenseUrl, _this.$scope.detail.selectedFileDrivingLicense, 'DrivingLicense'); },
                    uploaderVehicleLicense: function () { return _this.upload(_this.$scope.detail.submitVehicleLicenseUrl, _this.$scope.detail.selectedFileVehicleLicense, 'VehicleLicense'); },
                    click: _this.editReviseUser
                };
            });
            $("#f").bind('change', 'VehicleLicense', _this.handleFileSelect);
            $('#uploaderDriving').bind('change', 'uploaderDriving', _this.handleFileSelect);
            $('#idCard').bind('change', 'IDCard', _this.handleFileSelect);
            $('#realHead').bind('change', 'RealHead', _this.handleFileSelect);
        };
        this.handleFileSelect = function (e) {
            var files = e.target.files;
            switch (e.data) {
                case 'VehicleLicense':
                    return _this.$scope.detail.selectedFileVehicleLicense = files[0],
                        _this.$scope.detail.submitVehicleLicenseUrl = _this.handleFileSelectfunc('VehicleLicense', _this.$scope.detail.selectedFileVehicleLicense.name, files);
                case 'uploaderDriving':
                    return _this.$scope.detail.selectedFileDrivingLicense = files[0],
                        _this.$scope.detail.submitDrivingLicenseUrl = _this.handleFileSelectfunc('DrivingLicense', _this.$scope.detail.selectedFileDrivingLicense.name, files);
                case 'IDCard':
                    return _this.$scope.detail.selectedFileIdCard = files[0],
                        _this.$scope.detail.submitIdCardUrl = _this.handleFileSelectfunc('IDCard', _this.$scope.detail.selectedFileIdCard.name, files);
                case 'RealHead':
                    return _this.$scope.detail.selectedFileRealHead = files[0],
                        _this.$scope.detail.submitRealHeadUrl = _this.handleFileSelectfunc('RealHead', _this.$scope.detail.selectedFileRealHead.name, files);
            }
        };
        /**
         * 上传
         */
        this.upload = function (submitUri, selectedFile, result) {
            _this.azureBlob.upload({
                baseUrl: submitUri,
                sasToken: 'avatar-container',
                file: selectedFile,
                progress: function (data, status) {
                    console.log('test progress data', data);
                    console.log('test progress status', status);
                },
                complete: function (data, status) {
                    console.log('test complete data', data);
                    console.log('test complete status', status);
                    console.info('result', result);
                    this.uploadedFun(result);
                    switch (result) {
                        case 'RealHead': return $('#RealHeadUploadSuccess').css('color', '#5CB85C'), this.$scope.detail.realHeadImage = submitUri;
                        case 'IDCard': return $('#IDCardUploadSuccess').css('color', '#5CB85C'), this.$scope.detail.idCardImage = submitUri;
                        case 'DrivingLicense': return $('#DrivingLicenseUploadSuccess').css('color', '#5CB85C'), this.$scope.detail.drivingLicenseImage = submitUri;
                        case 'VehicleLicense': return $('#VehicleLicenseUploadSuccess').css('color', '#5CB85C'), this.$scope.detail.vehicleLicenseImage = submitUri;
                    }
                },
                error: function (xhr, desc, err) {
                    console.log('test error desc', desc);
                    console.log('test error err', err);
                },
                blockSize: 1024 * 32,
            });
        };
        this.init();
    }
    UserReviseController.prototype.init = function () {
        var _this = this;
        _super.prototype.init.call(this);
        this.$scope.reviseUser = this.showReviseUserDialog;
        this.$scope.queryTerms = {
            searchingKeyWord: "",
            searchingCarCode: "",
            searchingStartTime: moment("0001-01-01").toDate(),
            searchingEndTime: new Date(),
            searchingRoleId: "",
            searchingState: null,
            useCache: false
        };
        this.$scope.gridOptions = {
            enableSorting: false,
            useExternalPagination: true,
            //useExternalSorting: true,
            enableColumnResizing: true,
            paginationPageSizes: [10, 20, 30],
            columnDefs: [
                { name: "序号", field: "index", enableColumnMenu: false },
                { name: "车牌号", field: "carNumber", enableColumnMenu: false },
                { name: "车型", field: "carType", enableColumnMenu: false },
                { name: "车长", field: "carLength", enableColumnMenu: false },
                { name: "用户名", field: "userName", enableColumnMenu: false },
                { name: "真实姓名", field: "realName", enableColumnMenu: false },
                { name: "实名认证状态", field: "realNameState", enableColumnMenu: false },
                { name: "实名申请时间", field: "certificationTime", enableColumnMenu: false },
                { name: "禁用", field: "isLockedout", enableColumnMenu: false },
                { name: "角色", field: "roles", enableColumnMenu: false },
                {
                    name: "操作",
                    cellFilter: "checkmark",
                    cellTemplate: '<a data-toggle="modal" data-target="#myModal" class="fa fa-edit m-l-xs primary text-info" title="编辑" ng-click="grid.appScope.reviseUser(row.entity)"></a>',
                    enableColumnMenu: false
                }
            ],
            onRegisterApi: function (gridApi) {
                _this.gridApi = gridApi;
                gridApi.pagination.on.paginationChanged(_this.$scope, _this.paginationChanged);
            }
        };
        this.query();
        this.loadRolesCheckBox();
        this.showReviseUserDialog(this.$stateParams);
    };
    UserReviseController.prototype.loadData = function (queryTerms, skip, count) {
        var _this = this;
        this.userService.getList(queryTerms.searchingKeyWord, queryTerms.searchingCarCode, queryTerms.searchingStartTime, queryTerms.searchingEndTime, queryTerms.searchingRoleId, queryTerms.searchingState, queryTerms.useCache, skip, count).then(function (result) {
            _this.$scope.gridOptions.totalItems = result.data.total;
            _this.$scope.gridOptions.data = result.data.list;
            if (skip == 0) {
                _this.$scope.gridOptions.paginationCurrentPage = 1;
            }
            _this.$scope.querying = false;
        }, function (error) {
            _this.$scope.querying = false;
        });
    };
    UserReviseController.prototype.editReviseUser = function () {
        var _this = this;
        $ngBootbox.confirm("确定保存该数据吗？").then(function (e) {
            _this.userService.putUserRealNameCertificationStatus(_this.$scope.detail.userId, _this.$scope.detail.carCode, _this.$scope.detail.drivingLicenseCertificated, _this.$scope.detail.realHeadImageCertificated, _this.$scope.detail.idCardCertificated, _this.$scope.detail.vehicleLicenseCertificated).then(function (result) {
                _this.query();
            });
        });
    };
    UserReviseController.prototype.handleFileSelectfunc = function (imageName, name, files) {
        var baseUrl = this.$scope.detail.sasUrl;
        var indexOfQueryStart = baseUrl.indexOf("?");
        var objUrl = this.getObjectURL(files[0]);
        $('#' + imageName).attr('src', objUrl);
        $('#uploader' + imageName).css('display', 'inline');
        return baseUrl.substring(0, indexOfQueryStart) + '/' + name + baseUrl.substring(indexOfQueryStart);
    };
    /**
     * 建立一個可存取到該file的url
     */
    UserReviseController.prototype.getObjectURL = function (file) {
        var urlImg = null;
        if (window.URL.createObjectURL != undefined) {
            urlImg = window.URL.createObjectURL(file);
        }
        else if (window.URL != undefined) {
            urlImg = window.URL.createObjectURL(file);
        }
        return urlImg;
    };
    /**
     * 获取SAS url
     */
    UserReviseController.prototype.getSasUrl = function () {
        var _this = this;
        this.storageService.AquireStorageSharedSigniture('avatar-container').then(function (result) {
            _this.$scope.detail.sasUrl = result.data.StorageSharedSigniture;
        });
    };
    /**
     * 分解roles的传值
     */
    UserReviseController.prototype.rolesChecked = function (roles) {
        if (Object.prototype.toString.call(roles) == "[object Null]") {
            return [];
        }
        else if (Object.prototype.toString.call(roles) == "[object String]") {
            return roles.split(',');
        }
        else if (Object.prototype.toString.call(roles) == "[object Array]") {
            return roles;
        }
    };
    /**
     * ------------------分割
     */
    /**
     * 认证状态函数
     */
    UserReviseController.prototype.statePassCheck = function (way, statusValue, passValue, dispassValue) {
        if (statusValue == 'RealHeadImage') {
            switch (way) {
                case 'pass':
                    return this.$scope.detail.clickOkPassState = passValue,
                        this.$scope.detail.realHeadImageCertificated = this.getStateResult(this.$scope.detail.clickRemovePassState, this.$scope.detail.clickOkPassState);
                case 'remove':
                    return this.$scope.detail.clickRemovePassState = dispassValue,
                        this.$scope.detail.realHeadImageCertificated = this.getStateResult(this.$scope.detail.clickRemovePassState, this.$scope.detail.clickOkPassState);
            }
        }
        else if (statusValue == 'IDCardImage') {
            switch (way) {
                case 'pass':
                    return this.$scope.detail.clickIdCardOkPassState = passValue,
                        this.$scope.detail.idCardCertificated = this.getStateResult(this.$scope.detail.clickIdCardRemovePassState, this.$scope.detail.clickIdCardOkPassState);
                case 'remove':
                    return this.$scope.detail.clickIdCardRemovePassState = dispassValue,
                        this.$scope.detail.idCardCertificated = this.getStateResult(this.$scope.detail.clickIdCardRemovePassState, this.$scope.detail.clickIdCardOkPassState);
            }
        }
        else if (statusValue == 'DrivingLicenseImage') {
            switch (way) {
                case 'pass':
                    return this.$scope.detail.clickDrivingLicenseOkPassState = passValue,
                        this.$scope.detail.drivingLicenseCertificated = this.getStateResult(this.$scope.detail.clickDrivingLicenseRemovePassState, this.$scope.detail.clickDrivingLicenseOkPassState);
                case 'remove':
                    return this.$scope.detail.clickDrivingLicenseRemovePassState = dispassValue,
                        this.$scope.detail.drivingLicenseCertificated = this.getStateResult(this.$scope.detail.clickDrivingLicenseRemovePassState, this.$scope.detail.clickDrivingLicenseOkPassState);
            }
        }
        else if (statusValue == 'vehicleLicenseImage') {
            switch (way) {
                case 'pass':
                    return this.$scope.detail.clickVehicleLicenseOkPassState = passValue,
                        this.$scope.detail.vehicleLicenseCertificated = this.getStateResult(this.$scope.detail.clickVehicleLicenseRemovePassState, this.$scope.detail.clickVehicleLicenseOkPassState);
                case 'remove':
                    return this.$scope.detail.clickVehicleLicenseRemovePassState = dispassValue,
                        this.$scope.detail.vehicleLicenseCertificated = this.getStateResult(this.$scope.detail.clickVehicleLicenseRemovePassState, this.$scope.detail.clickVehicleLicenseOkPassState);
            }
        }
    };
    /**
     * 实名认证状态转换（提交数据）
     */
    UserReviseController.prototype.getStateResult = function (clickRemovePassState, clickOkPassState) {
        if (clickRemovePassState == true && clickOkPassState == true) {
            return 2;
        }
        else if (clickRemovePassState == false) {
            return 4;
        }
        else if (clickOkPassState == false) {
            return 3;
        }
    };
    ;
    return UserReviseController;
}(ListController));
app.controller('userReviseController', ['$scope', 'userService', 'rolesService', 'valueService', 'storageService', '$ngBootbox', 'azureBlob', '$stateParams', UserReviseController]);
